﻿using HutongGames.PlayMaker;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

[ExecuteInEditMode, AddComponentMenu("PlayMaker/PlayMakerGUI")]
public class PlayMakerGUI : MonoBehaviour
{
    [CompilerGenerated]
    private static bool HideCursor__BackingField;
    [CompilerGenerated]
    private static bool LockCursor__BackingField;
    [CompilerGenerated]
    private static Texture MouseCursor__BackingField;
    public bool controlMouseCursor = true;
    public bool drawStateLabels = true;
    public bool enableGUILayout = true;
    private static bool enableStateLabels = true;
    public bool filterLabelsWithDistance;
    private static float fsmLabelIndex;
    private static readonly List<PlayMakerFSM> fsmList = new List<PlayMakerFSM>();
    private static Color guiBackgroundColor = Color.white;
    private static Color guiColor = Color.white;
    private static Color guiContentColor = Color.white;
    private static Matrix4x4 guiMatrix = Matrix4x4.identity;
    private static UnityEngine.GUISkin guiSkin;
    public bool GUITextStateLabels = true;
    public bool GUITextureStateLabels = true;
    private static PlayMakerGUI instance;
    private static Texture2D labelBackground;
    private static readonly GUIContent labelContent = new GUIContent();
    private static GameObject labelGameObject;
    private static GUIStyle labelStyle;
    public float maxLabelDistance = 10f;
    private const float MaxLabelWidth = 200f;
    private static readonly List<PlayMakerFSM> previewFsmList = new List<PlayMakerFSM>();
    public bool previewOnGUI = true;
    private static readonly List<PlayMakerFSM> SortedFsmList = new List<PlayMakerFSM>();

    public static void AddPreviewFsm(PlayMakerFSM fsm)
    {
        if (!previewFsmList.Contains(fsm))
        {
            previewFsmList.Add(fsm);
        }
    }

    public static void ClearPreviewFsmList()
    {
        previewFsmList.Clear();
    }

    private static void DoEditGUI()
    {
        fsmList.Clear();
        fsmList.AddRange(previewFsmList);
        foreach (PlayMakerFSM rfsm in fsmList)
        {
            if (((rfsm != null) && rfsm.enabled) && ((rfsm.Fsm != null) && (rfsm.Fsm.EditState != null)))
            {
                foreach (FsmStateAction action in rfsm.Fsm.EditState.Actions)
                {
                    if (action.Active)
                    {
                        action.OnGUI();
                    }
                }
            }
        }
    }

    private void DrawStateLabel(PlayMakerFSM fsm)
    {
        if (labelStyle == null)
        {
            InitLabelStyle();
            if (labelStyle == null)
            {
                return;
            }
        }
        if ((Camera.main != null) && (fsm.gameObject != Camera.main))
        {
            if (fsm.gameObject == labelGameObject)
            {
                fsmLabelIndex++;
            }
            else
            {
                fsmLabelIndex = 0f;
                labelGameObject = fsm.gameObject;
            }
            string str = GenerateStateLabel(fsm);
            if (!string.IsNullOrEmpty(str))
            {
                Vector2 vector = new Vector2();
                labelContent.text = str;
                Vector2 vector2 = labelStyle.CalcSize(labelContent);
                vector2.x = Mathf.Clamp(vector2.x, 10f, 200f);
                if (this.GUITextureStateLabels && (fsm.gameObject.guiTexture != null))
                {
                    vector.x = (fsm.gameObject.transform.position.x * Screen.width) + fsm.gameObject.guiTexture.pixelInset.x;
                    vector.y = (fsm.gameObject.transform.position.y * Screen.height) + fsm.gameObject.guiTexture.pixelInset.y;
                }
                else if (this.GUITextStateLabels && (fsm.gameObject.guiText != null))
                {
                    vector.x = fsm.gameObject.transform.position.x * Screen.width;
                    vector.y = fsm.gameObject.transform.position.y * Screen.height;
                }
                else
                {
                    if (this.filterLabelsWithDistance && (Vector3.Distance(Camera.main.transform.position, fsm.transform.position) > this.maxLabelDistance))
                    {
                        return;
                    }
                    if (Camera.main.transform.InverseTransformPoint(fsm.transform.position).z <= 0f)
                    {
                        return;
                    }
                    vector = Camera.main.WorldToScreenPoint(fsm.transform.position);
                    vector.x -= vector2.x * 0.5f;
                }
                vector.y = (Screen.height - vector.y) - (fsmLabelIndex * 15f);
                Color backgroundColor = GUI.backgroundColor;
                Color color = GUI.color;
                int index = 0;
                if (fsm.Fsm.ActiveState != null)
                {
                    index = fsm.Fsm.ActiveState.ColorIndex;
                }
                Color color3 = Fsm.StateColors[index];
                GUI.backgroundColor = new Color(color3.r, color3.g, color3.b, 0.5f);
                GUI.contentColor = Color.white;
                GUI.Label(new Rect(vector.x, vector.y, vector2.x, vector2.y), str, labelStyle);
                GUI.backgroundColor = backgroundColor;
                GUI.color = color;
            }
        }
    }

    private void DrawStateLabels()
    {
        SortedFsmList.Clear();
        foreach (PlayMakerFSM rfsm in PlayMakerFSM.FsmList)
        {
            if (rfsm.gameObject.active)
            {
                SortedFsmList.Add(rfsm);
            }
        }
        SortedFsmList.Sort(delegate (PlayMakerFSM x, PlayMakerFSM y) {
            return string.Compare(x.gameObject.name, y.gameObject.name);
        });
        labelGameObject = null;
        foreach (PlayMakerFSM rfsm2 in SortedFsmList)
        {
            if (rfsm2.Fsm.ShowStateLabel)
            {
                this.DrawStateLabel(rfsm2);
            }
        }
    }

    private static string GenerateStateLabel(PlayMakerFSM fsm)
    {
        if (fsm.Fsm.ActiveState == null)
        {
            return "[DISABLED]";
        }
        return fsm.Fsm.ActiveState.Name;
    }

    private static void InitLabelStyle()
    {
        if (labelBackground != null)
        {
            UnityEngine.Object.Destroy(labelBackground);
        }
        labelBackground = new Texture2D(1, 1);
        labelBackground.SetPixel(0, 0, Color.white);
        labelBackground.Apply();
        GUIStyle style = new GUIStyle();
        style.normal.background = labelBackground;
        style.normal.textColor = Color.white;
        style.fontSize = 10;
        style.alignment = TextAnchor.MiddleLeft;
        style.padding = new RectOffset(4, 4, 1, 1);
        labelStyle = style;
    }

    private void OnDisable()
    {
        if (instance == this)
        {
            instance = null;
        }
    }

    private void OnEnable()
    {
        if (instance != null)
        {
            Debug.LogWarning("A scene should only have one PlayMakerGUI!");
        }
        instance = this;
        this.previewOnGUI = false;
    }

    private void OnGUI()
    {
        base.useGUILayout = this.enableGUILayout;
        if (GUISkin != null)
        {
            GUI.skin = GUISkin;
        }
        GUI.color = GUIColor;
        GUI.backgroundColor = GUIBackgroundColor;
        GUI.contentColor = GUIContentColor;
        if (this.previewOnGUI && !Application.isPlaying)
        {
            DoEditGUI();
        }
        fsmList.Clear();
        fsmList.AddRange(PlayMakerFSM.FsmList);
        foreach (PlayMakerFSM rfsm in fsmList)
        {
            if (((rfsm != null) && rfsm.Active) && (rfsm.Fsm.ActiveState != null))
            {
                foreach (FsmStateAction action in rfsm.Fsm.ActiveState.Actions)
                {
                    if (action.Active)
                    {
                        action.OnGUI();
                    }
                }
            }
        }
        if (Application.isPlaying && (Event.current.type == EventType.Repaint))
        {
            Matrix4x4 matrix = GUI.matrix;
            GUI.matrix = Matrix4x4.identity;
            if (MouseCursor != null)
            {
                Rect position = new Rect(Input.mousePosition.x - (MouseCursor.width * 0.5f), (Screen.height - Input.mousePosition.y) - (MouseCursor.height * 0.5f), (float) MouseCursor.width, (float) MouseCursor.height);
                GUI.DrawTexture(position, MouseCursor);
            }
            if (this.drawStateLabels && EnableStateLabels)
            {
                this.DrawStateLabels();
            }
            GUI.matrix = matrix;
            GUIMatrix = Matrix4x4.identity;
            if (this.controlMouseCursor)
            {
                if (Screen.lockCursor != LockCursor)
                {
                    Screen.lockCursor = LockCursor;
                }
                if (Screen.showCursor == HideCursor)
                {
                    Screen.showCursor = !HideCursor;
                }
            }
        }
    }

    public static void ResetInstance()
    {
        instance = null;
    }

    public static bool Enabled
    {
        get
        {
            return ((instance != null) && instance.enabled);
        }
    }

    public static bool EnableStateLabels
    {
        get
        {
            return enableStateLabels;
        }
        set
        {
            enableStateLabels = value;
        }
    }

    public static Color GUIBackgroundColor
    {
        get
        {
            return guiBackgroundColor;
        }
        set
        {
            guiBackgroundColor = value;
        }
    }

    public static Color GUIColor
    {
        get
        {
            return guiColor;
        }
        set
        {
            guiColor = value;
        }
    }

    public static Color GUIContentColor
    {
        get
        {
            return guiContentColor;
        }
        set
        {
            guiContentColor = value;
        }
    }

    public static Matrix4x4 GUIMatrix
    {
        get
        {
            return guiMatrix;
        }
        set
        {
            guiMatrix = value;
        }
    }

    public static UnityEngine.GUISkin GUISkin
    {
        get
        {
            return guiSkin;
        }
        set
        {
            guiSkin = value;
        }
    }

    public static bool HideCursor
    {
        [CompilerGenerated]
        get
        {
            return HideCursor__BackingField;
        }
        [CompilerGenerated]
        set
        {
            HideCursor__BackingField = value;
        }
    }

    public static PlayMakerGUI Instance
    {
        get
        {
            if (instance == null)
            {
                instance = (PlayMakerGUI) UnityEngine.Object.FindObjectOfType(typeof(PlayMakerGUI));
                if (instance == null)
                {
                    instance = new GameObject("PlayMakerGUI").AddComponent<PlayMakerGUI>();
                }
            }
            return instance;
        }
    }

    public static bool LockCursor
    {
        [CompilerGenerated]
        get
        {
            return LockCursor__BackingField;
        }
        [CompilerGenerated]
        set
        {
            LockCursor__BackingField = value;
        }
    }

    public static Texture MouseCursor
    {
        [CompilerGenerated]
        get
        {
            return MouseCursor__BackingField;
        }
        [CompilerGenerated]
        set
        {
            MouseCursor__BackingField = value;
        }
    }

    public static List<PlayMakerFSM> PreviewFsmList
    {
        get
        {
            return previewFsmList;
        }
    }
}

